from rest_framework import permissions

from users.models import CustomUser


class IsBuyerOrArtist(permissions.BasePermission):
    """
    自定义权限仅允许买家查看自己的订单，并允许艺术家查看与其艺术品相关的订单。
    """
    def has_permission(self, request, view):
        if request.method == 'POST':
            # 只有买家才能创建订单
            return request.user.role == CustomUser.BUYER
        return True

    # def has_object_permission(self, request, view, obj):
    #     if request.user.role == CustomUser.BUYER:
    #         # 买家只能看到自己的订单列表
    #         return obj.buyer == request.user
    #     elif request.user.role == CustomUser.ARTIST:
    #         # 艺术家只能看到自己发布的作品的订单
    #         return obj.artwork.artist == request.user
    #     return False